package com.haier.cloudremote.common;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {
	
	/**
	 * 发送带附件的邮件
	 * @param str_to_mail 发邮件的目的地（收件人信箱）  
	 * @param str_from_mail 发邮件的出发地（发件人的信箱）  
	 * @param str_smtp 发件人使用发邮件的电子信箱服务器
	 * @param str_user 发件人用户名
	 * @param str_pass 发件人密码
	 * @param str_title 邮件题目
	 * @param str_body 邮件正文
	 * @param str_file_path 附件文件目录
	 */
	public void send_mail_file(String str_to_mail,
			String str_from_mail,String str_smtp,
			String str_user,String str_pass,String str_title,
			String str_body,String str_file_path) { 
		
        SLog.v("lengfeng","send_mail_file"); 
 
        String host = str_smtp;   //发件人使用发邮件的电子信箱服务器  
        String from = str_from_mail;    //发邮件的出发地（发件人的信箱）  
        String to   = str_to_mail;   //发邮件的目的地（收件人信箱）  
 
       SLog.v("lengfeng",str_smtp); 
        SLog.v("lengfeng",str_from_mail); 
        SLog.v("lengfeng",str_to_mail); 
         
        Properties props = System.getProperties();// Get system properties          
        props.put("mail.smtp.host", host);// Setup mail server       
        props.put("mail.smtp.auth", "true"); //这样才能通过验证  
 
        MyAuthenticator myauth = new MyAuthenticator(str_user, str_pass);// Get session          
        Session session = Session.getDefaultInstance(props, myauth); 
 
        
        MimeMessage message = new MimeMessage(session); // Define message  
        
        try { 
            message.setFrom(new InternetAddress(from)); // Set the from address  
        } catch (AddressException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        }  
         
        try { 
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set the to address  
        } catch (AddressException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			} 
        } 
 
         
        try { 
            message.setSubject(str_title);// Set the subject  
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        } 
 
         
        try { 
            message.setText(str_body);// Set the content  
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        } 
 
         
        MimeBodyPart attachPart = new MimeBodyPart();  
        FileDataSource fds = new FileDataSource(str_file_path); //打开要发送的文件  
        try { 
            attachPart.setDataHandler(new DataHandler(fds)); 
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			} 
        }  
        try { 
            attachPart.setFileName(fds.getName()); 
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        }  
         
         
        MimeMultipart allMultipart = new MimeMultipart("mixed"); //附件  
        try { 
            allMultipart.addBodyPart(attachPart);//添加  
            
            //设置content里的内容
            MimeBodyPart contentMbp = new MimeBodyPart();
    		//请指定字符集，否则会是乱码
    		contentMbp.setContent(str_body, "text/html; charset=utf-8");
    		allMultipart.addBodyPart(contentMbp); //添加附件
    		
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			} 
        }  
        try { 
            message.setContent(allMultipart); 
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        }  
        try { 
            message.saveChanges(); 
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			} 
        }  
 
 
        try { 
            Transport.send(message);//开始发送  
        } catch (MessagingException e) { 
        	if(e!=null){
				SLog.e("ssc", "SendMail--------62------e===="+e.getMessage());
			}
        }         
         
	}  
}
